package rigby.projects.wikipediawebcrawler;

import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class WebCrawler {
	
	/**crawlUrl method
	 * 		Connects to & parses an initial URL 
	 * 		Adds it to ArrayList visitedSites
	 * 		Gets a new link from Document
	 * 		Checks visitedSites for the new link
	 * 			If found, calls restartRandomCrawl & clears visitedSites
	 * 		Adds newLink to visitedSites ArrayList
	 * 		Increments the jumpNum
	 * 		Prints newLink & sets urlToCrawl = newLink, & loops
	 * 
	 * @param urlToCrawl
	 * 		String URL used to initiate the loop
	 * @return void
	 */
	public void crawlUrl(String urlToCrawl){
		ArrayList<String> visitedSites = new ArrayList<String>();
		WebCrawlerDataMethods validLinkGetter = new WebCrawlerDataMethods();
		int jumpNum = 0;
		String newLink = null;
		
		String initialLink = urlToCrawl;
		System.out.println(initialLink);
		visitedSites.add(initialLink);
		
		while(true){
			try {
				Document urlDoc = Jsoup.connect(urlToCrawl).get();
				
				newLink = validLinkGetter.getValidLink(urlDoc);
				
				if(visitedSites.contains(newLink)){
					restartRandomCrawl(jumpNum, initialLink);
					visitedSites.clear();
				}
				
				visitedSites.add(newLink);
				jumpNum++;
				System.out.println(newLink);
				urlToCrawl = newLink;
				
			} catch (IOException e) {
				System.out.println();
				System.out.println("Error at:" + initialLink);
				restartRandomCrawl(jumpNum, initialLink);
				visitedSites.clear();
			} 
			
			try {
				Thread.sleep(250);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	/**restartRandomCrawl method
	 * 		Prints out the start URL & the total number of jumps, then resets the number of jumps
	 * 		Lastly, writes the startURL & jumpNum to a file & restarts the crawlUrl method
	 * 
	 * @param jumpNum
	 * 		int number of jumps from start URL to loop
	 * @param initialLink
	 * 		String starting URL
	 * @return void
	 */
	public void restartRandomCrawl(int jumpNum, String initialLink){
		System.out.println();
		System.out.println("Start URL: " + initialLink);
		System.out.println("Number of jumps: " + jumpNum);
		
		String destinationPath = "/Eclipse_Documents/webCrawlerData.txt";
		FileDataProcessor fdp = new FileDataProcessor();
		fdp.writeToFile(destinationPath, initialLink, jumpNum);
		
		jumpNum = 0;
		
		System.out.println("Logged to: " + destinationPath);
		System.out.println();
		System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		System.out.println();
		
		WebCrawlerDataMethods randomLinkGetter = new WebCrawlerDataMethods();
		crawlUrl(randomLinkGetter.getRandomLink());
	}

}
